package com.gzx.plugin.tjzy.modular.repair.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzx.plugin.tjzy.modular.repair.entity.GzxRepairPhoneNumber;
import com.gzx.plugin.tjzy.modular.repair.entity.GzxRepairPhoneNumberDetail;
import com.gzx.plugin.tjzy.modular.repair.param.GzxRepairPhoneNumberIdParam;
import com.gzx.plugin.tjzy.modular.repair.param.GzxRepairPhoneNumberPageParam;
import com.gzx.plugin.tjzy.modular.repair.service.GzxRepairPhoneNumberService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.util.CommonExcelUtil;

import java.io.IOException;
import java.util.List;

/**
 * 失联修复
 */
@Slf4j
@RestController
@Validated
public class GzxRepairPhoneNumberController {

    @Resource
    private GzxRepairPhoneNumberService gzxRepairPhonenumberService;

    /**
     * 获取分页
     */
    @GetMapping("/tjzy/repair/page")
    public CommonResult<Page<GzxRepairPhoneNumber>> page(GzxRepairPhoneNumberPageParam gzxRepairPhonenumberPageParam) {
        return CommonResult.data(gzxRepairPhonenumberService.page(gzxRepairPhonenumberPageParam));
    }

    /**
     * 删除
     */
    @PostMapping("/tjzy/repair/delete/{id}")
    public CommonResult<Void> delete(@PathVariable long id) {
        gzxRepairPhonenumberService.delete(id);
        return CommonResult.ok();
    }

    /**
     * 获取详情
     */
    @PostMapping("/tjzy/repair/getRepairResult")
    public CommonResult<List<GzxRepairPhoneNumberDetail>> getRepairResult(@RequestBody GzxRepairPhoneNumberIdParam gzxRepairPhonenumberIdParam) {
        //加载修复结果
        gzxRepairPhonenumberService.loadRepairResult(gzxRepairPhonenumberIdParam.getId());
        return CommonResult.data(gzxRepairPhonenumberService.getDetailList(gzxRepairPhonenumberIdParam.getId()));
    }

    /**
     * 导入修复名单
     */
    @PostMapping("tjzy/repair/importExcel")
    public CommonResult<Void> importExcel(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        gzxRepairPhonenumberService.importExcel(multipartFile);
        return CommonResult.ok();
    }

    /**
     * 开始进行修复
     */

    @PostMapping("tjzy/repair/repair")
    public CommonResult<Void> repairNumber(@RequestBody GzxRepairPhoneNumberIdParam gzxRepairPhonenumberIdParam) {
        try {
            gzxRepairPhonenumberService.repairNumber(gzxRepairPhonenumberIdParam.getId());
            return CommonResult.ok();
        } catch (Exception e) {
            log.error("开始进行修复异常：", e);
            return CommonResult.error("开始进行修复异常");
        }
    }

    @PostMapping("/tjzy/repair/exportExcel")
    public void getRepairResult(@RequestBody GzxRepairPhoneNumberIdParam gzxRepairPhonenumberIdParam, HttpServletResponse response) {
        int id = gzxRepairPhonenumberIdParam.getId();
        List<GzxRepairPhoneNumberDetail> detailList = gzxRepairPhonenumberService.getDetailList(id);
        CommonExcelUtil.exportExcel("修复结果", detailList, GzxRepairPhoneNumberDetail.class, response);
    }

}
